package xyz.o0o0o.demo.AuthorizationServerDemo2.entity;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.util.SerializationUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;

import com.alibaba.fastjson.JSON;

import xyz.o0o0o.demo.AuthorizationServerDemo2.util.SerializeUtil;

public class OAuthAccessToken {
	private String token_id;
	private String token;
	private String authentication_id;
	private String user_name;
	private String client_id;
	private String authentication;
	private String refresh_token;

	public OAuthAccessToken() {
	}

	public OAuthAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
		String refreshToken = null;
		if (token.getRefreshToken() != null) {
			refreshToken = token.getRefreshToken().getValue();
		}

//		if (readAccessToken(token.getValue())!=null) {
//			removeAccessToken(token.getValue());
//		}
		
		this.token_id = extractTokenKey(token.getValue());
		this.token = SerializationUtils.serialize(token).toString();
		this.authentication_id =  new DefaultAuthenticationKeyGenerator().extractKey(authentication);
		this.user_name = authentication.isClientOnly() ? null : authentication.getName();
		this.client_id = authentication.getOAuth2Request().getClientId();
		this.authentication = SerializationUtils.serialize(authentication).toString();
		this.refresh_token = extractTokenKey(refreshToken);
		System.out.println( "OAuthAccessToken:" +  JSON.toJSON(this));
	}

	public String getToken_id() {
		return token_id;
	}

	public void setToken_id(String token_id) {
		this.token_id = token_id;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getAuthentication_id() {
		return authentication_id;
	}

	public void setAuthentication_id(String authentication_id) {
		this.authentication_id = authentication_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getClient_id() {
		return client_id;
	}

	public void setClient_id(String client_id) {
		this.client_id = client_id;
	}

	public String getAuthentication() {
		return authentication;
	}

	public void setAuthentication(String authentication) {
		this.authentication = authentication;
	}

	public String getRefresh_token() {
		return refresh_token;
	}

	public void setRefresh_token(String refresh_token) {
		this.refresh_token = refresh_token;
	}

	
	protected String extractTokenKey(String value) {
		if (value == null) {
			return null;
		}
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
		}
		catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");
		}

		try {
			byte[] bytes = digest.digest(value.getBytes("UTF-8"));
			return String.format("%032x", new BigInteger(1, bytes));
		}
		catch (UnsupportedEncodingException e) {
			throw new IllegalStateException("UTF-8 encoding not available.  Fatal (should be in the JDK).");
		}
	}
}
